//
//  LSZAudioCollectionViewController.m
//  JKFuYinProject
//
//  Created by 九酷 on 2018/5/25.
//  Copyright © 2018年 LC. All rights reserved.
//

#import "LSZAudioCollectionViewController.h"


@interface LSZAudioCollectionViewController ()
{
    BOOL _isOpen[1];

}
@property(nonatomic,strong) NSString * isEditor;//正常1   反之 2删除
//@property(nonatomic,strong) NSString * isAdSuccessful;
@end

@implementation LSZAudioCollectionViewController


#pragma mark - 得到歌单的数据  type 1专辑(2歌手备用)3歌曲
-(void)getSongData
{
    if([[_UserM usercode] isEqualToString:@""]&& [NullStringjudgment(NSStr(self.uid)) isEqualToString:@""]){
        //本人的账号  并且没登录的时候
        self.mutableArrayManager = [[NSMutableArray alloc]init];
        [self rowAnimation];
    }else{
        [LSZRequestResolutionDic getFavoritesList:@"3" uid:self.uid :^(BOOL isOk, NSString * string, LSZFavoritesListManager * manager) {
            if (isOk == YES) {
                self.mutableArrayManager=manager.arrayData;
            }
            [self rowAnimation];
        }];
    }
}
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:YES];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"音频";
    self.isEditor = @"1";
    self.mutableArrayManager = [[NSMutableArray alloc]init];
    [self getSongData];
    [self.tableViewCollection registerClass:[LSZPlaylistCollectionTableViewCell class] forCellReuseIdentifier:@"LSZPlaylistCollectionTableViewCell"];
    [self.tableViewCollection registerClass:[LSZSongCollectionHeaderCell class] forHeaderFooterViewReuseIdentifier:@"LSZSongCollectionHeaderCell"];
    [self.tableViewCollection registerClass:[LSZEditorAlbumDetailsTableViewCell class] forCellReuseIdentifier:@"LSZEditorAlbumDetailsTableViewCell"];

    NSString * strShareFavorites = [NSString stringWithFormat:@"%@CreateNewFavorites",@"LSZAudioCollectionViewController"];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(AudioCollectionFavorites) name:strShareFavorites object:nil];//新建收藏夹

    NSString * strPlaylist = [NSString stringWithFormat:@"%@PlaylistManagement",@"LSZAudioCollectionViewController"];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(AudioCollectionPlaylist) name:strPlaylist object:nil];//管理歌单

 }
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        return 1;
    }else{
        if (_isOpen[section] == NO){
            return self.mutableArrayManager.count;
        }
        else{
            tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
            return 0;
        }
    }
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (ScreenWidth == 320) {
        //5
        return 45;
    }else if(ScreenWidth == 375){
        // 6 7
        return 50;
    }
    else {
        //plus
        return 58;
    }
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        return 0.0001;
    }else{
        if (ScreenWidth == 320) {
            //5
            return 35;
        }else if(ScreenWidth == 375){
            // 6 7
            return 40;
        }
        else {
            //plus
            return 48;
        }
    }
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        UITableViewHeaderFooterView * cell = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"cell"];
        if (!cell) {
            cell = [[UITableViewHeaderFooterView alloc]initWithReuseIdentifier:@"cell"];
        }
        return cell;
    }else{
        LSZSongCollectionHeaderCell * cell = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"LSZSongCollectionHeaderCell"];
        [cell.butChange addTarget:self action:@selector(clickButChange) forControlEvents:UIControlEventTouchUpInside];
        [cell.butSetUp addTarget:self action:@selector(clickButSetUp) forControlEvents:UIControlEventTouchUpInside];
        [cell.imageViewChange tapGesture:^(UIGestureRecognizer *ges) {
            [self clickButChange];
        }];
        [cell.labelChange tapGesture:^(UIGestureRecognizer *ges) {
            [self clickButChange];
        }];
        [cell.imageSetUp tapGesture:^(UIGestureRecognizer *ges) {
            [self clickButSetUp];
        }];
        cell.imageViewChange.transform = _isOpen[section]?CGAffineTransformMakeRotation(-M_PI):CGAffineTransformIdentity;
        if([self.isEditor isEqualToString:@"1"]){
            cell.labelOk.hidden = YES;
            cell.imageSetUp.hidden = NO;
        }else{
            cell.labelOk.hidden = NO;
            cell.imageSetUp.hidden = YES;
        }
        if([(NullStringjudgment(NSStr(self.uid))) isEqualToString:@""]){
            cell.butSetUp.hidden = NO;
        }else{
            cell.butSetUp.hidden = YES;
        }
        return cell;
    }
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        LSZCollectionAndImageViewTableViewCell * cellNew = [tableView dequeueReusableCellWithIdentifier:@"cellNew"];
        if (!cellNew) {
            cellNew = [[LSZCollectionAndImageViewTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cellNew"];
        }
        cellNew.selectionStyle  = UITableViewCellSelectionStyleNone;
        return cellNew;
    }else{
        if ([self.isEditor  isEqualToString:@"1"]||([self.isEditor  isEqualToString:@"2"]&&indexPath.row==0)) {
            LSZFavoritesListMg  * mg = nil;
            mg = self.mutableArrayManager[indexPath.row];
            LSZPlaylistCollectionTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"LSZPlaylistCollectionTableViewCell" forIndexPath:indexPath];
            if(indexPath.row == 0){
                cell.imageViewCoverCollection.image = [UIImage imageNamed:@"收藏歌曲喜欢默认图片"];
                cell.labelTime.hidden = YES;
            }else{
                [cell.imageViewCoverCollection sd_setImageWithURL:[NSURL URLWithString:mg.image] placeholderImage:[UIImage imageNamed:@"我的_设置_关于"]];
                cell.labelTime.hidden = NO;
            }
            cell.labelTitle.text = mg.name;
            cell.numberSong.text = [NSString stringWithFormat:@"%@首",mg.total];
            cell.labelTime.text  = [NSString stringWithFormat:@"%@创建",mg.addtime];
            cell.selectionStyle  = UITableViewCellSelectionStyleNone;
            return cell;
        }else{
            LSZFavoritesListMg  * mg = nil;
            mg = self.mutableArrayManager[indexPath.row];
            LSZEditorAlbumDetailsTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"LSZEditorAlbumDetailsTableViewCell" forIndexPath:indexPath];
            [cell.imageViewCoverCollection sd_setImageWithURL:[NSURL URLWithString:mg.image] placeholderImage:[UIImage imageNamed:@"我的_设置_关于"]];
            cell.labelTitle.text = mg.name;
            cell.numberSong.text = [NSString stringWithFormat:@"%@首",mg.total];
            cell.labelTime.text  = [NSString stringWithFormat:@"%@创建",mg.addtime];
            cell.butDelete.tag = indexPath.row;
            [cell.butDelete addTarget:self action:@selector(clickDelete:) forControlEvents:UIControlEventTouchUpInside];
            cell.selectionStyle  = UITableViewCellSelectionStyleNone;
            return cell;
        }
    }
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        if([[_UserM usercode] isEqualToString:@""]&& [self.uid isEqualToString:@""]){
            pushAccLoginVC;
        }else{
            LSZAlbumArtistListViewController * vc = [[LSZAlbumArtistListViewController alloc]init];
            vc.fuid = self.uid;
            [self.navigationController pushViewController:vc animated:YES];
        }
    }else{
        LSZFavoritesListMg  * mg = nil;
        if (self.mutableArrayManager.count >indexPath.row) {
            mg = self.mutableArrayManager[indexPath.row];
            if (![mg.type isEqualToString:@"-1"]) {
                LSZFavoritesListDetailsViewController * vc = [[LSZFavoritesListDetailsViewController alloc]init];
                vc.id_id = mg.id_id;
                vc.type = @"3";
                vc.stringTitle = mg.name;
                vc.fuid = self.uid;
                [self.navigationController pushViewController:vc animated:YES];
            }
        }
    }
}
#pragma mark - 开合
-(void)clickButChange
{
    _isOpen[1] = !_isOpen[1];
    [self rowAnimation];
}
#pragma mark - 刷新第一个区
-(void)rowAnimation
{
    NSIndexSet *indexSet=[[NSIndexSet alloc]initWithIndex:1];
    [self.tableViewCollection reloadSections:indexSet withRowAnimation:UITableViewRowAnimationAutomatic];
}
#pragma mark  - 歌单设置
-(void)clickButSetUp
{
    if ([self.isEditor isEqualToString:@"1"]) {
        LSZPopoverViewController * vc = [[LSZPopoverViewController alloc]init];
        [vc show];
        [vc selectIndexRowWith:5];//布局
        [vc fromWhichInterface:@"LSZAudioCollectionViewController"];
    }else{
        self.isEditor = @"1";
        [self.tableViewCollection reloadData];
    }
}
#pragma mark- 新建歌单
-(void)AudioCollectionFavorites
{
    //提示框添加文本输入框
    UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"创建新文件夹"
                                                                   message:@""
                                                            preferredStyle:UIAlertControllerStyleAlert];

    UIAlertAction* okAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault
                                                     handler:^(UIAlertAction * action) {
                                                         for(UITextField *text in alert.textFields){
                                                             //响应事件//得到文本信息
                                                             NSLog(@"确定text = %@", text.text);
                                                             [self addNewFolder:text.text];
                                                         }
                                                     }];
    UIAlertAction* cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel
                                                         handler:^(UIAlertAction * action) {
                                                             //响应事件
                                                             NSLog(@"取消action = %@", alert.textFields);
                                                         }];
    [alert addTextFieldWithConfigurationHandler:^(UITextField *textField) {
        textField.placeholder = @"输入新建文件夹名字";
    }];
    [alert addAction:okAction];
    [alert addAction:cancelAction];
    [self presentViewController:alert animated:YES completion:nil];
}
#pragma mark - 添加新的文件夹名字
-(void)addNewFolder :(NSString *)string
{
    [LSZRequestData TheAdditionOfFavorites:@"3" name:string :^(BOOL isOK, NSString * string, NSDictionary * dic) {
        if (isOK == YES) {
            [self getSongData];
        }else{
            [self setStringWarning:string];
        }
    }];
}
#pragma mark - 管理歌单
-(void)AudioCollectionPlaylist
{
    self.isEditor = @"2";
    _isOpen[1] = NO;
    [self rowAnimation];
}
#pragma mark - 删除歌单列表数据
-(void)clickDelete:(UIButton *)but
{
    LSZFavoritesListMg  * mg = nil;
    mg = self.mutableArrayManager[but.tag];
    NSString * str = [[NSString alloc]init];
    if ([mg.total integerValue]>0) {
        str = @"收藏夹不为空,您确定要删除吗?";
    }else{
        str = @"您确定删除收藏夹吗?";
    }
    UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"温馨提示"
                                                                   message:str
                                                            preferredStyle:UIAlertControllerStyleAlert];

    UIAlertAction* cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault
                                                         handler:^(UIAlertAction * action) {
                                                             //响应事件
                                                             NSLog(@"action = %@", action);
                                                         }];
    UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault
                                                          handler:^(UIAlertAction * action) {
                                                              //响应事件
                                                              [self okFavoritesDeletion:but.tag];
                                                          }];
    [alert addAction:cancelAction];
    [alert addAction:defaultAction];
    [self presentViewController:alert animated:YES completion:nil];
}
-(void)okFavoritesDeletion :(NSInteger)numberRow
{
    LSZFavoritesListMg  * mg = nil;
    mg = self.mutableArrayManager[numberRow];
    [LSZRequestData favoritesDeletion:mg.id_id :^(BOOL isOk, NSString * string, NSDictionary * dic) {
        if (isOk == YES) {
            [self setStringWarning:@"删除成功"];
            [self.mutableArrayManager removeObjectAtIndex:numberRow];
            [self rowAnimation];
        }else{
            [self setStringWarning:string];
        }
    }];
}
-(UITableView *)tableViewCollection
{
    if (_tableViewCollection == nil) {
        _tableViewCollection = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight) style:UITableViewStylePlain];
        if([(NullStringjudgment(NSStr(self.uid))) isEqualToString:@""]){
            _tableViewCollection.frame = CGRectMake(0, 0, ScreenWidth, ScreenHeight);
        }else{
            _tableViewCollection.frame = CGRectMake(0, 0, ScreenWidth, ScreenHeight-NAVIGATION_BAR_HEIGHT-44);
        }
        _tableViewCollection.dataSource = self;
        _tableViewCollection.delegate = self;
        _tableViewCollection.separatorColor = divider;
        _tableViewCollection.estimatedRowHeight = 0;
        _tableViewCollection.estimatedSectionFooterHeight = 0;
        _tableViewCollection.estimatedSectionHeaderHeight = 0;
        _tableViewCollection.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
        [self.view addSubview:_tableViewCollection];
    }
    return _tableViewCollection;
}
@end
